﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;

namespace Report
{
    public partial class Form1 : Form
    {

        rptUyNhiemChi cr = new rptUyNhiemChi();
        SQL sql = new SQL();
        string number = "";
       
        public Form1()
        {
            InitializeComponent();
           
        }
        public Form1(string date, string id, string dvtt_name, string dvtt_id, string dvtt_bank, string dvtt_addr, string dvtt_amount, string dvh_name, string dvh_id, string dvh_bank, string dvh_addr, string dvh_amount, string content, string money,int include,int exclude)
            : this()
        {
            dateTimePicker1.Value = Convert.ToDateTime(date.Trim());
            tb_ID.Text = id.Trim();
            tb_dvtt_name.Text = dvtt_name.Trim();
            tb_dvtt_id.Text = dvtt_id.Trim();
            tb_dvtt_bank.Text = dvtt_bank.Trim();
            tb_dvtt_addr.Text = dvtt_addr.Trim();
            //.Text = dvtt_amount.Trim();
            tb_dvh_name.Text = dvh_name.Trim();
            tb_dvh_id.Text = dvh_id.Trim();
            tb_dvh_bank.Text = dvh_bank.Trim();
            tb_dvh_addr.Text = dvtt_addr.Trim();
            //tb_dvh_money.Text = dvh_amount.Trim();
            tb_content.Text = content.Trim();
            tb_amount.Text = money.Trim();
            btnSave.Text = "Xóa";
            setVisible(true);
            if (include == 1) checkBox1.Checked = true; else checkBox1.Checked = false;
            if (exclude == 1) checkBox2.Checked = true; else checkBox2.Checked = false;

        }
      

         private void setSqlParameter(string name, string value)
         {
             SqlParameter pr = new SqlParameter(name,value);

         }


       
         private void btnView_Click(object sender, EventArgs e)
         {
             try
             {



                 int ck = 0;
                 if (checkBox1.Checked == true) ck = 1;
                 if (checkBox2.Checked == true) ck = 2;

                 number = sql.ChuyenSo(tb_amount.Text.Trim());
                 sql.setParameter(tb_dvtt_name.Text.Trim(), "NameDVTT", cr);
                 sql.setParameter(tb_dvtt_addr.Text.Trim(), "DVTT_ADDR",cr);
                 sql.setParameter(tb_dvtt_id.Text.Trim(), "DVTT_ID",cr);
                 //sql.setParameter(Convert.ToDouble(tb_dvtt_money.Text.Trim()), "DVTT_AMOUNT",cr);
                 sql.setParameter(tb_dvtt_bank.Text.Trim(), "DVTT_BANK",cr);
                 sql.setParameter(tb_dvh_name.Text.Trim(), "NameDVH",cr);
                 sql.setParameter(tb_dvh_addr.Text.Trim(), "DVH_ADDR",cr);
                 sql.setParameter(tb_dvh_id.Text.Trim(), "DVH_ID",cr);
                 sql.setParameter(tb_dvh_bank.Text.Trim(), "DVH_BANK",cr);
                 //sql.setParameter(Convert.ToDouble(tb_dvh_money.Text.Trim()), "DVH_AMOUNT",cr);
                 sql.setParameter(Convert.ToDouble(tb_amount.Text.Trim()), "MONEY",cr);
                 sql.setParameter(tb_content.Text.Trim(), "CONTENT",cr);
                 sql.setParameter(Convert.ToInt32( tb_ID.Text.Trim()), "ID",cr);
                 sql.setParameter(dateTimePicker1.Value.ToString("dd/MM/yyyy"),"DateCreat",cr);
                 sql.setParameter(number.Trim(), "str_money", cr);
                 sql.setParameter(ck, "check", cr);
                 if((tb_ID.Text)!="" && btnSave.Text=="Save")
                 {
                     int n = Convert.ToInt32(tb_ID.Text.Trim());
                     if (n < 10) tb_ID.Text = "000" + (n + 1).ToString();
                     if (n >= 10 && n < 100) tb_ID.Text = "00" + (n + 1).ToString();
                     
                     
                     
                     
                 }

                 Form2 frm = new Form2(cr);
                 
                 frm.ShowDialog();
             }
             catch (Exception ex)
             {
                 MessageBox.Show("dữ liệu nhập vào không hợp lệ", ex.Message,MessageBoxButtons.OK,MessageBoxIcon.Error);
             }


         }

       

         private void btnSave_Click(object sender, EventArgs e)
         {
             try
             {
                 int include = 0;
                 int exclude = 0;
                 if (checkBox1.Checked == true) include = 1; else include = 0;
                 if (checkBox2.Checked == true) exclude = 1; else exclude = 0;
                 SqlParameter[] pr = new SqlParameter[15];
                 pr[0] = new SqlParameter("@Date", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
                 pr[1] = new SqlParameter("@ID", tb_ID.Text.Trim());
                 pr[2] = new SqlParameter("@DVTT_NAME", tb_dvh_name.Text.Trim());
                 pr[3] = new SqlParameter("@DVTT_ID", tb_dvtt_id.Text.Trim());
                 pr[4] = new SqlParameter("@DVTT_BANK", tb_dvtt_bank.Text.Trim());
                 //pr[5] = new SqlParameter("@DVTT_AMOUNT", tb_dvtt_money.Text.Trim());
                 pr[5] = new SqlParameter("@DVH_NAME", tb_dvh_name.Text.Trim());
                 pr[6] = new SqlParameter("@DVH_ID", tb_dvh_id.Text.Trim());
                 pr[7] = new SqlParameter("@DVH_BANK", tb_dvh_bank.Text.Trim());
                 //pr[9] = new SqlParameter("@DVH_AMOUNT", tb_dvh_money.Text.Trim());
                 pr[8] = new SqlParameter("@DVTT_ADDR", tb_dvtt_addr.Text.Trim());
                 pr[9] = new SqlParameter("@DVH_ADDR", tb_dvh_addr.Text.Trim());
                 pr[10] = new SqlParameter("@MONEY",Convert.ToDouble(tb_amount.Text.Trim()));
                 pr[11] = new SqlParameter("@CONTENT", tb_content.Text.Trim());
                 pr[12] = new SqlParameter("@Func", 1);
                 pr[13] = new SqlParameter("@INCLUDE", include);
                 pr[14] = new SqlParameter("@EXCLUDE", exclude);
                 if (btnSave.Text == "Save")
                 {
                     sql.execute_Query("sql_search_UNC", pr);
                     MessageBox.Show("Đã lưu vào trong CSDL");
                 }


                 SqlParameter[] prdel = new SqlParameter[3];
                 prdel[0] = new SqlParameter("@Func", 2);
                 prdel[1] = new SqlParameter("@ID", tb_ID.Text.Trim());
                 prdel[2] = new SqlParameter("@Date", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
                 if (btnSave.Text == "Xóa")
                 {
                     sql.execute_Query("sql_search_UNC", prdel);
                     MessageBox.Show("Xóa thành công");
                     Clear();
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("Dữ liệu nhập vào không hợp lệ",ex.Message,MessageBoxButtons.OK,MessageBoxIcon.Error);
             }



         }
         private void setVisible(bool t)
         {
             dateTimePicker1.Enabled = !t;
             tb_amount.ReadOnly = t;
             tb_content.ReadOnly=t;
             tb_dvh_addr.ReadOnly = t;
             tb_dvh_bank.ReadOnly = t;
             tb_dvh_id.ReadOnly = t;
             //tb_dvh_money.ReadOnly = t;
             tb_dvh_name.ReadOnly = t;
             tb_dvtt_addr.ReadOnly = t;
             tb_dvtt_bank.ReadOnly = t;
             tb_dvtt_id.ReadOnly = t;
             //tb_dvtt_money.ReadOnly = t;
             tb_dvtt_name.ReadOnly = t;
             tb_ID.ReadOnly = t;
             checkBox1.Enabled = !t;
             checkBox2.Enabled = !t;

             
         }

         private void btnExit_Click(object sender, EventArgs e)
         {
             this.Close();
         }

         private void Form1_Load(object sender, EventArgs e)
         {
             string id = "";
             DataTable dt = new DataTable();
             dt = sql.getDataTable("sql_search_UNC", new SqlParameter("@Func", 3));
             if (dt.Rows.Count > 0)
             {
                 id = dt.Rows[0][0].ToString();
             }
                if(btnSave.Text=="Save")         
                 if (id != "")
                 {   
                     int value = Convert.ToInt32(id) + 1;
                     if (value < 10) tb_ID.Text = "000" +value.ToString();
                     else tb_ID.Text = "00" + value.ToString();
                     
                 }
                 else
                 {
                     tb_ID.Text = id.Trim();
                 }
         }

         private void checkBox1_CheckedChanged(object sender, EventArgs e)
         {
             if (checkBox1.Checked == true) checkBox2.Checked = false;
         }

         private void checkBox2_CheckedChanged(object sender, EventArgs e)
         {
             if (checkBox2.Checked == true) checkBox1.Checked = false;
         }
         private void Clear()
         {
             tb_amount.Text = "";
             tb_content.Text = "";
             tb_dvh_addr.Text = "";
             tb_dvh_bank.Text = "";
             tb_dvh_id.Text = "";
             tb_dvh_name.Text = "";
             tb_dvtt_addr.Text = "";
             tb_dvtt_bank.Text = "";
             tb_dvtt_id.Text = "";
             tb_dvtt_name.Text = "";
             tb_ID.Text = "";
             checkBox1.Checked = false;
             checkBox2.Checked = false;
             dateTimePicker1.Refresh();

             

         }

         
        
    }
}
